package com.dmagic.cms.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dmagic.cms.common.dao.CommonDao;
import com.dmagic.cms.common.exception.ServiceException;
import com.dmagic.cms.common.exception.ValidateException;
import com.dmagic.cms.common.query.PageUtil;
import com.dmagic.cms.common.service.CommonService;
import com.dmagic.cms.common.util.StringUtil;
import com.dmagic.cms.dao.SuccessfulCaseDao;
import com.dmagic.cms.entity.LinkedHeatMap;
import com.dmagic.cms.entity.SuccessfulCase;
import com.dmagic.cms.service.HeatMapService;
import com.dmagic.cms.service.SuccessfulCaseService;

@Service
@Transactional(readOnly = true)
public class SuccessfulCaseServiceImpl extends CommonService<SuccessfulCase> implements SuccessfulCaseService {
	@Autowired
	private SuccessfulCaseDao successfulCaseDao;

	@Autowired
	private HeatMapService heatMapService;

	@Override
	protected CommonDao<SuccessfulCase, Serializable> getCommonDao() {
		return (CommonDao) successfulCaseDao;
	}

	@Override
	@Transactional(readOnly = false)
	public void saveSuccessfulCase(SuccessfulCase[] successfulCase) throws ValidateException {
		for (SuccessfulCase p : successfulCase) {
			if (StringUtil.isBlank(p.getSuccessfulCaseName())
					&& StringUtil.isBlank(p.getSuccessfulCaseContent())) {
				continue;
			}
			if (p.getIsTop() == null) {
				p.setIsTop(false);
			}
			if (p.getSuccessfulCaseStatus() == null) {
				p.setSuccessfulCaseStatus(false);
			}
			saveEntity(p);
		}
	}

	@Override
	public void searchSuccessfulCase(PageUtil<Map> pageUtil) throws ServiceException {
		try {
			successfulCaseDao.searchSuccessfulCase(pageUtil);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public SuccessfulCase getSuccessfulCase(Long successfulCaseId) throws ServiceException {
		return getEntityById(successfulCaseId);
	}

	public List<SuccessfulCase> getSuccessfulCaseBySuccessfulCaseNo(String successfulCaseNo) {
		return successfulCaseDao.findBy("successfulCaseNo", successfulCaseNo);
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteSuccessfulCase(List<Long> successfulCaseIds) throws ServiceException {
		for (Long successfulCaseId : successfulCaseIds) {
			deleteSuccessfulCase(successfulCaseId);
		}
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteSuccessfulCase(Long successfulCaseId) throws ServiceException {
		deleteEntityById(successfulCaseId);
		heatMapService.deleteLinkedHeatMapByLinkedIdAndType(successfulCaseId,
				LinkedHeatMap.HeatMapType.successfulCase.getValue());
	}

	@Override
	@Transactional(readOnly = false)
	public void changeSuccessfulCaseCategory(List<Long> successfulCaseIds, Long successfulCaseCategoryId)
			throws ServiceException {
		for (Long successfulCaseId : successfulCaseIds) {
			SuccessfulCase successfulCase = getSuccessfulCase(successfulCaseId);
			successfulCase.setSuccessfulCaseCategoryId(successfulCaseCategoryId);
			saveEntity(successfulCase);
		}
	}

}
